package com.ruoyi.logistic.utils;

import com.alibaba.fastjson2.JSONObject;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendBatchSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendBatchSmsResponse;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;


/** @version:（1.0）
* @ClassName	SMSPushUtils
* @Description: （短信验证服务）
* @date:2018年9月8日下午12:20:30
*/
@Component
public class SMSVerificationUtils {

	protected static Logger smsLogger=LoggerFactory.getLogger("SMSVerificationUtils");

	public static final String SMSFORMAT = "00";

	/**
	 * 声明静态属性-IP
	 */
	private static String signName;
	private static String accessKeyId;
	private static String accessKeySecret;
	private static String product;
	private static String domain;
	private static String templateCode;
	/**
	 * 通过 Value 注解获取 yml 中配置的值
	 */
	@Value("${smsconfig.signName}")
	private String name;
	@Value("${smsconfig.accessKeyId}")
	private String keyId;
	@Value("${smsconfig.accessKeySecret}")
	private String keySecret;
	@Value("${smsconfig.product}")
	private String products;
	@Value("${smsconfig.domain}")
	private String domains;
	@Value("${smsconfig.templateCode}")
	private String templateCodes;

	@PostConstruct
	public void transValues() {
		signName = this.name;
		accessKeyId = this.keyId;
		accessKeySecret = this.keySecret;
		product = this.products;
		domain = this.domains;
		templateCode = this.templateCodes;
	}


	public static SendSmsResponse sendSms(String telephone, String code, String areaCode) throws ClientException {
		telephone = areaCode + telephone;
		// 可自助调整超时时间
		System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
		System.setProperty("sun.net.client.defaultReadTimeout", "10000");

		String signNames = signName;
		// 初始化acsClient,暂不支持region化
		IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId,
				accessKeySecret);
		DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product,
				domain);
		IAcsClient acsClient = new DefaultAcsClient(profile);

		// 组装请求对象-具体描述见控制台-文档部分内容
		SendSmsRequest request = new SendSmsRequest();
		// 必填:待发送手机号
		smsLogger.info("格式化手机号 :  00 + 国际区号 + 号码   ==========>"+"   SMSFORMAT :"+ SMSFORMAT +"  areaCode: " + areaCode);
		request.setPhoneNumbers(SMSFORMAT + telephone);// 接收号码格式为00+国际区号+号码
		// 必填:短信签名-可在短信控制台中找到
		/*配置文件中文乱码待解决*/

//		request.setSignName(getSmsConfig().getSignname());
		request.setSignName(signName);
		// 必填:短信模板-可在短信控制台中找到
		request.setTemplateCode(templateCode);
		if ("86".equals(areaCode) || "886".equals(areaCode) || "852".equals(areaCode))
			request.setTemplateCode(templateCode);

		// 可选:模板中的变量替换JSON串,如模板内容为"亲爱的用户,您的验证码为${code}"时,此处的值为
		request.setTemplateParam("{\"code\":\"" + code + "\"}");

		// 选填-上行短信扩展码(无特殊需求用户请忽略此字段)
		// request.setSmsUpExtendCode("90997");

		// 可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
		// request.setOutId("yourOutId");

		// hint 此处可能会抛出异常，注意catch
		SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
		smsLogger.info("阿里云短信服务回执详情："+ JSONObject.toJSONString(sendSmsResponse));
		if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) {
			smsLogger.info("短信发送成功！");
		} else {
			smsLogger.info("短信发送失败！");
		}
		return sendSmsResponse;
	}

	public static SendBatchSmsResponse SendBatchSms(String[] telephone, String params) throws ClientException {
		// 可自助调整超时时间
		System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
		System.setProperty("sun.net.client.defaultReadTimeout", "10000");

		// 初始化acsClient,暂不支持region化
		IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId,
				accessKeySecret);
		DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product,
				domain);
		IAcsClient acsClient = new DefaultAcsClient(profile);

		// 组装请求对象-具体描述见控制台-文档部分内容
		SendBatchSmsRequest request = new SendBatchSmsRequest();
		// 必填:待发送手机号
		String s = telephone.toString();
		String s1 = params.toString();
		request.setPhoneNumberJson(s);// 接收号码格式为00+国际区号+号码
		request.setTemplateParamJson(s1);
		request.setSignNameJson(signName);
		// 必填:短信模板-可在短信控制台中找到
		request.setTemplateCode(templateCode);


		// 选填-上行短信扩展码(无特殊需求用户请忽略此字段)
		// request.setSmsUpExtendCode("90997");

		// 可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者
		// request.setOutId("yourOutId");

		// hint 此处可能会抛出异常，注意catch
		SendBatchSmsResponse response = new SendBatchSmsResponse();
//		SendBatchSmsResponse response = acsClient.getAcsResponse(request);
//		smsLogger.info("阿里云短信服务回执详情："+ JSONObject.toJSONString(response));
//		if (response.getCode() != null && response.getCode().equals("OK")) {
//			smsLogger.info("短信发送成功！");
//		} else {
//			smsLogger.info("短信发送失败！");
//		}
		return response;
	}
}
